﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;

[CustomEditor(typeof(HoverInformation))]
public class DrawHoverInformation : Editor {

    HoverInformation hoverInformation;
    SerializedObject obj;
    SerializedProperty component;
    SerializedProperty property;
    List<string> fieldsList = new List<string>();
    int index = 0;

    private void OnEnable()
    {
        obj = new SerializedObject(target);
        component = obj.FindProperty("component");
        property = obj.FindProperty("property");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        hoverInformation = (HoverInformation)target;

        hoverInformation.component = EditorGUILayout.ObjectField("信息脚本",hoverInformation.component, typeof(Component), true) as Component;
        if (hoverInformation.component != null)
        {
            fieldsList.Clear();
            var fields = hoverInformation.component.GetType().GetFields();
            foreach (var item in fields)
            {
                fieldsList.Add(item.Name);
            }
            var properties = hoverInformation.component.GetType().GetProperties();
            foreach (var item in properties)
            {
                fieldsList.Add(item.Name);
            }
            int i = fieldsList.IndexOf(hoverInformation.property);
            index = EditorGUILayout.Popup("属性名称", i, fieldsList.ToArray());
            if (fieldsList.Count > 0 && index >= 0)
            {
                hoverInformation.property = fieldsList[index];
            }
        }
        serializedObject.ApplyModifiedProperties();
    }
}
